﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using TimetableSystem.Model;
using System.Windows.Media;
using System.Windows;

namespace TimetableSystem
{
    /// <summary>
    /// A grid which contains TextBlocks representing lecture slots, the TextBlock is either empty or filled with lecture information
    /// </summary>
    public partial class LectureTimetable : UserControl
    {
        private List<Module> _modules;

        public LectureTimetable()
        {
            InitializeComponent();
        }

        public List<Module> Modules
        {
            get { return _modules; }
            set
            {
                _modules = value;

                PopulateTimetable();
            }
        }

        private void PopulateTimetable()
        {
            GridTimetable.Children.Clear();

            //Labels
            string[] daysOfWeek = new string[5] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
            for (int i = 1; i <= 5; i++)
            {
                var label = new Label() { FontWeight = FontWeights.Bold };
                label.Content = daysOfWeek[i - 1];
                Grid.SetColumn(label, i);
                Grid.SetRow(label, 0);
                GridTimetable.Children.Add(label);
            }
            string[] timetableSlots = new string[9] { "9.15", "10.15", "11.15", "12.15", "13.15", "14.15", "15.15", "16.15", "17.15" };
            for (int i = 1; i <= 9; i++)
            {
                var label = new Label() { FontWeight = FontWeights.Bold };
                label.Content = timetableSlots[i - 1];
                Grid.SetColumn(label, 0);
                Grid.SetRow(label, i);
                GridTimetable.Children.Add(label);
            }

            //Days - start at 1 to miss out the period labels
            for (int i = 1; i <= 5; i++)
            {
                //Periods - start at 1 to miss out the lecture labels
                for (int j = 1; j <= 9; j++)
                {
                    var label = new Label();
                    Grid.SetColumn(label, i);
                    Grid.SetRow(label, j);
                    GridTimetable.Children.Add(label);
                }
            }

            if (Modules != null && Modules.Count > 0)
            {
                foreach (Module module in Modules)
                {
                    if (module.Lessons != null && module.Lessons.Count > 0)
                    {
                        foreach (Lesson lesson in module.Lessons)
                        {
                            var label = new Label();
                            label.Content = module.Title + "\n" + lesson.Location + " (" + module.ModuleCode + ")";
                            label.Background = new SolidColorBrush(Colors.Gray);
                            label.Foreground = new SolidColorBrush(Colors.White);
                            SetLabelAlignmentFromLesson(label, lesson);
                            GridTimetable.Children.Add(label);
                        }
                    }
                }
            }
        }

        private void SetLabelAlignmentFromLesson(Label label, Lesson lesson)
        {
            int gridColumn = GetColumnFromLessonDay(lesson.StartTime.DayOfWeek);
            int gridRow = GetRowFromLessonTime(lesson.StartTime);
            int gridRowSpan = GetRowSpanFromLessonTime(lesson.StartTime, lesson.EndTime);

            Grid.SetColumn(label, gridColumn);
            Grid.SetRow(label, gridRow);
            Grid.SetRowSpan(label, gridRowSpan);
        }

        private int GetColumnFromLessonDay(DayOfWeek dayOfWeek)
        {
            if (dayOfWeek == DayOfWeek.Monday)
                return 1;
            else if (dayOfWeek== DayOfWeek.Tuesday)
                return 2;
            else if (dayOfWeek== DayOfWeek.Wednesday)
                return 3;
            else if (dayOfWeek== DayOfWeek.Thursday)
                return 4;
            else if (dayOfWeek== DayOfWeek.Friday)
                return 5;
            else
                return -1;
        }

        private int GetRowFromLessonTime(DateTime lessonStart)
        {
            if (lessonStart.Hour == 9)
                return 1;
            else if (lessonStart.Hour == 10)
                return 2;
            else if (lessonStart.Hour == 11)
                return 3;
            else if (lessonStart.Hour == 12)
                return 4;
            else if (lessonStart.Hour == 13)
                return 5;
            else if (lessonStart.Hour == 14)
                return 6;
            else if (lessonStart.Hour == 15)
                return 7;
            else if (lessonStart.Hour == 16)
                return 8;
            else if (lessonStart.Hour == 17)
                return 9;
            else
                return -1; 
        }

        private int GetRowSpanFromLessonTime(DateTime lessonStart, DateTime lessonEnd)
        {
            int lessonLength = lessonEnd.Hour - lessonStart.Hour;

            if (lessonLength <= 0)
                return -1;

            return lessonLength;
        }
    }
}
